package com.example.wisdomclassroomapi.common.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class EntityToWrapperUtil {
    public static <T> QueryWrapper<T> toQueryWrapper(Class<T> cls, QueryWrapper<T> queryWrapper, Object entity) {
        Field[] fields = cls.getDeclaredFields();
        Map<String, Object> conditions = new HashMap();
        Field[] var5 = fields;
        int var6 = fields.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            Field f = var5[var7];
            f.setAccessible(true);

            try {
                if (f.get(entity) != null) {
                    conditions.put(f.getName().replaceAll("[A-Z]", "_$0").toLowerCase(), f.get(entity));
                }
            } catch (Exception var10) {
            }
        }

        if (conditions.size() == 0) {
            return null;
        } else {
            Iterator var11 = conditions.keySet().iterator();

            while(var11.hasNext()) {
                String key = (String)var11.next();
                queryWrapper.and((i) -> {
                    i.eq(key, conditions.get(key));
                });
            }

            return queryWrapper;
        }
    }
}
